package com.xk857.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xk857.entities.SysRole;
import com.xk857.system.req.SysRoleREQ;
import com.xk857.system.service.ISysRoleService;
import com.xk857.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 角色信息表 前端控制器
 *
 * @author CV大魔王
 * @since 2021-02-25
 */
@Api(value = "角色管理接口", tags = "角色管理接口")
@RestController
@RequestMapping("/pri/role")
public class SysRoleController {


    @Autowired
    private ISysRoleService sysRoleService;


    @ApiOperation("根据角色名称查询列表接口")
    @PostMapping("/search")
    public IPage<SysRole> search(@RequestBody SysRoleREQ req) {
        return sysRoleService.queryPage(req);
    }

    @ApiOperation("新增角色信息接口")
    @PostMapping
    public Result save(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.ok();
    }


    @ApiOperation("查询角色详情接口")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id) {
        return Result.ok(sysRoleService.getById(id));
    }


    @ApiOperation("修改角色信息接口")
    @PutMapping
    public Result update(@RequestBody SysRole sysRole) {
        // 更新时间
        sysRole.setUpdateDate(new Date());
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }


    @ApiOperation("根据角色id查询拥有的菜单ids接口")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true)
    @GetMapping("/{id}/menu/ids")
    public List<String> findMenuIdsById(@PathVariable("id") String id) {
        return sysRoleService.findMenuIdsById(id);
    }


    @ApiOperation("通过角色ID删除角色接口")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        return sysRoleService.deleteById(id);
    }


    // allowMultiple=true 表示数组格式的参数,dataType="String" 表示数组中参数的类型
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "角色ID", required = true), @ApiImplicitParam(name = "menuIds", value = "菜单id集合", required = true, allowMultiple = true, dataType = "String")})
    @ApiOperation("新增角色菜单权限数据接口")
    @PostMapping("/{id}/menu/save")
    public Result saveRoleMenu(@PathVariable("id") String id, @RequestBody List<String> menuIds) {
        return sysRoleService.saveRoleMenu(id, menuIds);
    }


}
